/**
 * 网络请求方法
 * 
 * @param params
 *            参数
 * @param url
 *            请求地址
 * @param successCallBack
 *            成功回调
 */
function ajaxUtils(params, url, successCallBack) {
	$.ajax({
		type : "POST",
		data : params,
		url : url,
		async : true,
		dataType : "json",
		success : function (result) {
			successCallBack(strToJson(result));
		},
		failure : function(result) {
			showError(result);
		},
	})
}
function ajaxUtilsNoAsync(params, url, successCallBack) {
	$.ajax({
		type : "POST",
		data : params,
		url : url,
		async : false,
		dataType : "json",
		success : function (result) {
			successCallBack(strToJson(result));
		},
		failure : function(result) {
			showError(result);
		},
	})
}
function strToJson(obj) {
	var isjson = typeof (obj) == "object"
			&& Object.prototype.toString.call(obj).toLowerCase() == "[object object]"
			&& !obj.length;
	if (isjson) {
		return obj;
	} else {
		return jQuery.parseJSON(obj);
	}
}
/**
 * 跳转页面
 * 
 * @param pagePath
 */
function openPage(pagePath) {
	window.location.href = pagePath;
}
/**
 * json对象转换为table表格
 * 
 * @param jsonResult
 * @param tableId
 * @param otherParams
 * @returns {String}
 */
function jsonToTable(jsonResult, tableId, otherParams) {
	var string = "<table id='"
			+ tableId
			+ "' "
			+ otherParams
			+ " class='table table-bordered' data-resizable-columns-id='demo-table' border='1px'cellspacing='0' cellpadding='0'>";
	var length = jsonResult.length;
	if (length > 0) {
		string = string + "<tr>";
		jQuery.each(jsonResult[0], function(key, val) {
			string = string + "<th style='text-align: center;'>" + key
					+ "</th>";
		});
		string = string + "</tr>";
	}
	for (var i = 0; i < length; i++) {
		string = string + "<tr>";
		jQuery.each(jsonResult[i], function(key, val) {
			if (val == 'null') {
				val = "";
			}
			string = string + "<td style='text-align: center;'><div>" + val
					+ "</div></td>";
		});
		string = string + "</tr>";
	}
	string = string + "</table>";
	return string;
}
var nowShowdialogId;
var nowLeftMargin;
function showDialog(dialogId) {
	nowShowdialogId = dialogId;
	$(document.body).css({
		"overflow-x" : "hidden",
		"overflow-y" : "hidden"
	});
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var dialogBg = $("body #windowsDialogBg");
	if (dialogBg == null || dialogBg.length == 0) {
		$("body")
				.append(
						"<div id=\"windowsDialogBg\" style=\"position: absolute;left:0px;top:0px;background-color:rgba(0, 0, 0, 0.3);width:"
								+ windowWidth
								+ "px;height:"
								+ windowHeight
								+ "px\"></div>");
	}
	var dialogView = $("#" + dialogId);
	$("#windowsDialogBg").append(dialogView);
	// 先清除之前dialog的边距信息，防止设置新边距的时候出现偏差
	$("#windowsDialogBg #" + dialogId).css({
		"display" : "block",
		"left" : 0,
		"top" : 0
	});
	var dialogWidth = $("#windowsDialogBg #" + dialogId).outerWidth(true);
	var dialogHeight = $("#windowsDialogBg #" + dialogId).outerHeight(true);
	var leftMargin = (windowWidth - dialogWidth) / 2;
	nowLeftMargin = leftMargin;
	var leftTop = (windowHeight - dialogHeight) / 2;
	var scrollTop = $(document).scrollTop();
	$("#windowsDialogBg #" + dialogId).css({
		"display" : "block",
		"left" : leftMargin,
		"top" : -1000
	});
	$("#windowsDialogBg #" + dialogId).animate({
		"display" : "block",
		"left" : leftMargin,
		"top" : leftTop
	});
}
function cancelDialog(callBack) {
	$("#windowsDialogBg #" + nowShowdialogId).animate({
		"display" : "block",
		"left" : nowLeftMargin,
		"top" : -1000
	}, function animateEnd() {
		$("#windowsDialogBg").children().css({
			"display" : "none"
		});
		$("body").append($("#windowsDialogBg").children());
		$("#windowsDialogBg").remove();
		$(document.body).css({
			"overflow-x" : "auto",
			"overflow-y" : "auto"
		});
		callBack();
	});
}
/**
 * 弹出正确的toast
 * 
 * @param msg
 *            提示文字
 * @returns 返回当前提示的div对象
 */
function showSuccess(msg) {
	var toastView = showToast(msg);
	toastView.css({
		"background-color" : "rgba(0, 255, 0, 1)",
		"color" : "rgba(255, 255, 255, 1)",
	});
	return toastView;
}
/**
 * 弹出错误的toast
 * 
 * @param msg
 *            提示文字
 * @returns 返回当前提示的div对象
 */
function showError(msg) {
	var toastView = showToast(msg);
	toastView.css({
		"background-color" : "rgba(255, 0, 0, 1)",
		"color" : "rgba(255, 255, 255, 1)",
	});
	return toastView;
}
function showToast(msg) {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var dialogBg = $("body #toastView");
	if (dialogBg == null || dialogBg.length == 0) {
		$("body").append(
				"<div id=\"toastView\" style=\"position: absolute;width:"
						+ windowWidth + "px;height:auto\">" + msg + "</div>");
	} else {
		$("#toastView").text(msg);
	}
	$("#toastView").css({
		"display" : "block",
		"margin" : "0px",
		"left" : "0px",
		"bottom" : "0px",
		"line-height" : "30px",
		"text-align" : "center"

	});
	setTimeout("toastHide()", 2000);
	return $("#toastView");
}
/**
 * 隐藏提示框
 */
function toastHide() {
	$("#toastView").remove();
}
